import { DataTypes, Model, Sequelize } from 'sequelize';

export interface ComponentAttributes {
  id?: number;
  name: string;
  category: string;
  description?: string;
  version?: string;
  framework?: 'react' | 'vue' | 'angular';
  popularity_score?: number;
  search_keywords?: string;
  created_at?: Date;
  updated_at?: Date;
}

export interface ComponentCreationAttributes extends Omit<ComponentAttributes, 'id' | 'created_at' | 'updated_at'> {}

export class Component extends Model<ComponentAttributes, ComponentCreationAttributes> implements ComponentAttributes {
  public id!: number;
  public name!: string;
  public category!: string;
  public description?: string;
  public version?: string;
  public framework?: 'react' | 'vue' | 'angular';
  public popularity_score?: number;
  public search_keywords?: string;
  
  // timestamps!
  public readonly created_at!: Date;
  public readonly updated_at!: Date;

  // associations
  public props?: ComponentProp[];
  public examples?: ComponentExample[];
  public imports?: ComponentImport[];

  static initModel(sequelize: Sequelize): typeof Component {
    return Component.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        name: {
          type: DataTypes.STRING(100),
          allowNull: false,
          unique: true,
        },
        category: {
          type: DataTypes.STRING(50),
          allowNull: false,
        },
        description: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        version: {
          type: DataTypes.STRING(20),
          defaultValue: '5.0',
        },
        framework: {
          type: DataTypes.ENUM('react', 'vue', 'angular'),
          defaultValue: 'react',
        },
        popularity_score: {
          type: DataTypes.INTEGER,
          defaultValue: 0,
        },
        search_keywords: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
      },
      {
        sequelize,
        tableName: 'components',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        indexes: [
          {
            fields: ['category'],
          },
          {
            fields: ['name'],
          },
          {
            fields: ['popularity_score'],
          },
          {
            type: 'FULLTEXT',
            fields: ['name', 'description', 'search_keywords'],
          },
        ],
      }
    );
  }
}

export interface ComponentPropAttributes {
  id?: number;
  component_id: number;
  name: string;
  type: string;
  required?: boolean;
  description?: string;
  default_value?: string;
  options?: string[] | null;
  created_at?: Date;
}

export interface ComponentPropCreationAttributes extends Omit<ComponentPropAttributes, 'id' | 'created_at'> {}

export class ComponentProp extends Model<ComponentPropAttributes, ComponentPropCreationAttributes> implements ComponentPropAttributes {
  public id!: number;
  public component_id!: number;
  public name!: string;
  public type!: string;
  public required?: boolean;
  public description?: string;
  public default_value?: string;
  public options?: string[] | null;
  
  public readonly created_at!: Date;

  static initModel(sequelize: Sequelize): typeof ComponentProp {
    return ComponentProp.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        component_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
          references: {
            model: 'components',
            key: 'id',
          },
          onDelete: 'CASCADE',
        },
        name: {
          type: DataTypes.STRING(100),
          allowNull: false,
        },
        type: {
          type: DataTypes.STRING(200),
          allowNull: false,
        },
        required: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
        },
        description: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        default_value: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        options: {
          type: DataTypes.JSON,
          allowNull: true,
        },
      },
      {
        sequelize,
        tableName: 'component_props',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        indexes: [
          {
            fields: ['component_id'],
          },
        ],
      }
    );
  }
}

export interface ComponentExampleAttributes {
  id?: number;
  component_id: number;
  title: string;
  description?: string;
  code: string;
  preview_url?: string;
  framework?: 'react' | 'vue' | 'angular';
  created_at?: Date;
}

export interface ComponentExampleCreationAttributes extends Omit<ComponentExampleAttributes, 'id' | 'created_at'> {}

export class ComponentExample extends Model<ComponentExampleAttributes, ComponentExampleCreationAttributes> implements ComponentExampleAttributes {
  public id!: number;
  public component_id!: number;
  public title!: string;
  public description?: string;
  public code!: string;
  public preview_url?: string;
  public framework?: 'react' | 'vue' | 'angular';
  
  public readonly created_at!: Date;

  static initModel(sequelize: Sequelize): typeof ComponentExample {
    return ComponentExample.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        component_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
          references: {
            model: 'components',
            key: 'id',
          },
          onDelete: 'CASCADE',
        },
        title: {
          type: DataTypes.STRING(200),
          allowNull: false,
        },
        description: {
          type: DataTypes.TEXT,
          allowNull: true,
        },
        code: {
          type: DataTypes.TEXT('long'),
          allowNull: false,
        },
        preview_url: {
          type: DataTypes.STRING(500),
          allowNull: true,
        },
        framework: {
          type: DataTypes.ENUM('react', 'vue', 'angular'),
          defaultValue: 'react',
        },
      },
      {
        sequelize,
        tableName: 'component_examples',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        indexes: [
          {
            fields: ['component_id', 'framework'],
          },
        ],
      }
    );
  }
}

export interface ComponentImportAttributes {
  id?: number;
  component_id: number;
  import_statement: string;
  framework?: 'react' | 'vue' | 'angular';
  created_at?: Date;
}

export interface ComponentImportCreationAttributes extends Omit<ComponentImportAttributes, 'id' | 'created_at'> {}

export class ComponentImport extends Model<ComponentImportAttributes, ComponentImportCreationAttributes> implements ComponentImportAttributes {
  public id!: number;
  public component_id!: number;
  public import_statement!: string;
  public framework?: 'react' | 'vue' | 'angular';
  
  public readonly created_at!: Date;

  static initModel(sequelize: Sequelize): typeof ComponentImport {
    return ComponentImport.init(
      {
        id: {
          type: DataTypes.INTEGER,
          primaryKey: true,
          autoIncrement: true,
        },
        component_id: {
          type: DataTypes.INTEGER,
          allowNull: false,
          references: {
            model: 'components',
            key: 'id',
          },
          onDelete: 'CASCADE',
        },
        import_statement: {
          type: DataTypes.STRING(500),
          allowNull: false,
        },
        framework: {
          type: DataTypes.ENUM('react', 'vue', 'angular'),
          defaultValue: 'react',
        },
      },
      {
        sequelize,
        tableName: 'component_imports',
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        indexes: [
          {
            fields: ['component_id', 'framework'],
          },
        ],
      }
    );
  }
}